package com.guigui.gulimall.product.controller;

import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.R;
import com.guigui.gulimall.product.dto.AttrRelationAddDto;
import com.guigui.gulimall.product.entity.AttrEntity;
import com.guigui.gulimall.product.entity.AttrGroupEntity;
import com.guigui.gulimall.product.service.AttrAttrgroupRelationService;
import com.guigui.gulimall.product.service.AttrGroupService;
import com.guigui.gulimall.product.service.AttrService;
import com.guigui.gulimall.product.vo.AttrRelationVo;
import com.guigui.gulimall.product.vo.BrandWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author xin
 * @email 806748502@qq.com
 * @date 2021-07-23 15:09:03
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @GetMapping("/{catelogId}/withattr")
    public R getBrandWithAttr(@PathVariable("catelogId") Long catelogId) {
        List<BrandWithAttrVo> brandWithAttrVos = attrGroupService.getBrandWithAttrByCatelogId(catelogId);

        return R.ok().put("data", brandWithAttrVos);
    }

    /**
     * 批量保存关联关系
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrRelationAddDto> attrRelationAddDtos){

        attrAttrgroupRelationService.saveRelationBatch(attrRelationAddDtos);

        return R.ok();
    }

    /**
     * 查询指定attr关联信息
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntityList = attrService.getAttrRelation(attrgroupId);
        return R.ok().put("data",attrEntityList);
    }

    /**
     * 查询未关联的attr信息
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getAttrNoRelation(@RequestParam Map<String, Object> params,
                               @PathVariable("attrgroupId") Long attrgroupId){

        PageUtils page = attrService.getAttrNoRelation(params, attrgroupId);

        return R.ok().put("page",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){
        PageUtils page = attrGroupService.queryPage(params,catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getInfoById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrRelationVo> attrRelationVos) {
        attrAttrgroupRelationService.deleteAttrBatch(attrRelationVos);
        return R.ok();
    }

}
